
from io import BytesIO
import time
from sys import argv

import pptx
from pptx.dml.color import RGBColor
from pptx.enum.text import PP_PARAGRAPH_ALIGNMENT
from pptx.util import Pt, Cm


def pptrun(inputprs, tempprs):
    inputprslist = [(1, 1), (2, 1), (3, 1), (2, 4), (6, 1), (7, 1), (8, 2), (8, 8)]  # 输入数据源 表格位置 列表
    tempprslist = [(0, 1), (1, 1), (1, 3), (0, 3), (4, 0), (4, 2), (6, 1)]  # 模板表格 位置 列表
    for i in range(len(inputprs.slides)):  # PPT页数
        print('正在处理第' + str(i+1) + '页')
        pptrun3sum = 1
        for ii in range(len(inputprs.slides[i].shapes)):  # 每页元素

            if inputprs.slides[i].shapes[ii].has_table:  # 判断元素是否是表格，是继续执行
                inputprstable = inputprs.slides[i].shapes[ii].table  # 表格赋值给变了
                for tempprsshapes in tempprs.slides[i].shapes:
                    if tempprsshapes.has_table:
                        tempprstable = tempprsshapes.table
                        for iii in range(len(tempprslist)):  # 循环 取值列表，
                            if iii == len(tempprslist)-1:
                                text11 = inputprstable.cell(inputprslist[iii][0], inputprslist[iii][1]).text
                                text11 = text11 + inputprstable.cell(inputprslist[iii+1][0], inputprslist[iii+1][1]).text
                                text11 = text11.replace('\n', '')  # 处理回车
                                text11 = text11.replace('\x0b', '')  # 处理特殊符号
                                tempprstablecell = tempprstable.cell(tempprslist[iii][0], tempprslist[iii][1])
                                textcopyblack(tempprstablecell, text11)

                            else:
                                text11 = inputprstable.cell(inputprslist[iii][0], inputprslist[iii][1]).text  # 取对应单元格值
                                text11 = text11.replace('\n', '')  # 处理回车
                                text11 = text11.replace('\x0b', '')  # 处理特殊符号
                                tempprstablecell = tempprstable.cell(tempprslist[iii][0], tempprslist[iii][1])  # 输出模板单元格位置
                                if iii < len(tempprslist)-3:  # 如果 输入源位置小于3，启用 金色
                                    textcopy(tempprstablecell, text11)
                                else:  # 否则 黑色
                                    textcopyblack(tempprstablecell, text11)
                        pptrun2(inputprstable, tempprstable)  # 调用 改善类别模块
            else:
                try:
                    inputprs.slides[i].shapes[ii].image
                    pptrun3(inputprs.slides[i].shapes[ii], tempprs.slides[i], pptrun3sum)
                    pptrun3sum += 1

                except:
                    print('第'+str(i)+"页，可能有误，请检查")

    savefilename = time.strftime('%Y年%m月%d日%H.%M.%S', time.localtime())+'.pptx'
    tempprs.save('.\\ppt\\'+savefilename)
    print("处理完毕")


def pptrun2(inputprstable, tempprstable):  # 改善类别模块

    inlist = []
    for i in range(7, 16):  # 取值单元格 列序号
        inlist.append(inputprstable.cell(4, i).text.rstrip())  # 取值，加入列表
    temcell = tempprstable.cell(2, 1)  # 赋值模板表格位置
    alist = ["成本        ", "效率       ", "质量        ", "交期        ", "安全        ", "5S        ", "系统        ", "其他"]
    temcell.text = ""  # 清空表格
    for i in temcell.text_frame.paragraphs:  # 段落赋值给 i
        for ii in range(len(alist)):  # 循环 取 输入值
            i.add_run()  # 添加文字模块
            i.runs[ii].text = alist[ii]  # 文字内容
            i.runs[ii].font.bold = False
            i.runs[ii].font.name = "微软雅黑 (正文)"
            if ii < 3:
                if len(inlist[ii]) > 0:  # 判断 取值列表 内容是否 为 "空"
                    i.runs[ii].font.size = Pt(14)
                    i.runs[ii].font.color.rgb = RGBColor(255, 0, 0)
                else:
                    i.runs[ii].font.size = Pt(14)
                    i.runs[ii].font.color.rgb = RGBColor(127, 96, 0)
            if 3 < ii < 6:

                if len(inlist[ii + 2]) > 0:   # 判断 取值列表 内容是否 为 "空"
                    i.runs[ii].font.size = Pt(14)
                    i.runs[ii].font.color.rgb = RGBColor(255, 0, 0)
                else:
                    i.runs[ii].font.size = Pt(14)
                    i.runs[ii].font.color.rgb = RGBColor(127, 96, 0)
            if ii == 6:
                for iii in range(3, 6):
                    if len(inlist[iii]) > 0:  # 判断 取值列表 内容是否 为 '空'
                        i.runs[ii].font.size = Pt(14)
                        i.runs[ii].font.color.rgb = RGBColor(255, 0, 0)
                        break
                    else:
                        i.runs[ii].font.size = Pt(14)
                        i.runs[ii].font.color.rgb = RGBColor(127, 96, 0)
            if ii == 7:
                # if inlist[8] == "√":  # 判断 取值列表 内容是否 为 √
                if inlist[8] != '':  # 判断 取值列表 内容是否 为 √
                    i.runs[ii].font.size = Pt(14)
                    i.runs[ii].font.color.rgb = RGBColor(255, 0, 0)
                else:
                    i.runs[ii].font.size = Pt(14)
                    i.runs[ii].font.color.rgb = RGBColor(127, 96, 0)
            if ii == 3:
                i.runs[ii].font.size = Pt(14)
                i.runs[ii].font.color.rgb = RGBColor(127, 96, 0)
        i.alignment = PP_PARAGRAPH_ALIGNMENT.CENTER  # 居中
    return


def pptrun3(inputprsshapes, tempprsslides, i):  # 图片处理
    if i == 1:
        tempprsslides.shapes.add_picture(BytesIO(inputprsshapes.image.blob), Cm(0.62), Cm(8.66), Cm(16.32), Cm(7.15))
    if i == 2:
        tempprsslides.shapes.add_picture(BytesIO(inputprsshapes.image.blob), Cm(17.02), Cm(8.66), Cm(16.23),
                                         Cm(7.15))

    return


def textcopyblack(tempprstablecell, text11):  # 黑色字体处理
    tempprstablecell.text = text11
    tempprstablecell.text_frame.paragraphs[0].font.size = Pt(14)
    tempprstablecell.text_frame.paragraphs[0].font.name = "微软雅黑 (正文)"
    tempprstablecell.text_frame.paragraphs[0].font.color.rgb = RGBColor(0, 0, 0)
    tempprstablecell.text_frame.paragraphs[0].font.bold = False


def textcopy(tempprstablecell, text11):  # 主要字体处理
    tempprstablecell.text = text11
    tempprstablecell.text_frame.paragraphs[0].font.size = Pt(12)
    tempprstablecell.text_frame.paragraphs[0].font.name = "微软雅黑 (正文)"
    tempprstablecell.text_frame.paragraphs[0].alignment = PP_PARAGRAPH_ALIGNMENT.CENTER
    tempprstablecell.text_frame.paragraphs[0].font.color.rgb = RGBColor(127, 96, 0)
    tempprstablecell.text_frame.paragraphs[0].font.bold = False


def prttable(prs):  # 输出PPTX 表格 位置 及文字  平时不使用
    iii = 1
    for a in range(len(prs.slides)):
        print("这是第：" + str(iii) + "页")
        for shape in prs.slides[a].shapes:
            # print(shape)
            if shape.has_text_frame:
                text_frame = shape.text_frame
                for aa in text_frame.paragraphs:
                    print(aa.text)
            if shape.has_table:
                for i in range(len(shape.table.rows)):
                    for ii in range(len(shape.table.columns)):
                        print(str(i) + '，' + str(ii) + " : " + shape.table.cell(i, ii).text)
            else:
                print(shape)
            # if shape.picture
        iii += 1
        if a > 4:
            break
        # break


if __name__ == '__main__':
    if len(argv) > 1:
        inputname = argv[1]
    else:
        inputname = '.\\ppt\\input.pptx'
    try:
        inputprs = pptx.Presentation(inputname)  # 载入源
        tempprs = pptx.Presentation('.\\ppt\\tem.pptx')  # 载入模板
        pptrun(inputprs, tempprs)  # 启用表格处理
        # prttable(inputprs)  # 表格内容对应编号
    except pptx.exc.PackageNotFoundError as aaa:
        print("源文件或模板错误，错误信息："+str(aaa))



